package com.primeton.esper.extention;

import java.util.HashMap;
import java.util.Map;

import com.espertech.esper.collection.Pair;
import com.espertech.esper.epl.agg.aggregator.AggregationMethod;
import com.espertech.esper.epl.agg.service.AggSvcGroupAllNoAccessImpl;
import com.espertech.esper.epl.agg.service.AggregationService;
import com.espertech.esper.epl.core.ResultSetProcessor;
import com.espertech.esper.epl.core.ResultSetProcessorAggregateAll;
import com.primeton.utils.SerializerUtil;

public class AggregationServiceManager {

	private static HashMap<String, AggregationMethod[]> services = SerializerUtil.getInstance().deSerializeMap();

	public static void addService(Pair<ResultSetProcessor, AggregationService> service) {
		System.out.println("-----------------------------------------------------");
		System.out.println(service.getFirst().getClass());
		if (service.getFirst() instanceof ResultSetProcessorAggregateAll) {
			ResultSetProcessorAggregateAll processor = (ResultSetProcessorAggregateAll) service.getFirst();
			AggregationService s = service.getSecond();
			if (s instanceof AggSvcGroupAllNoAccessImpl) {
				AggSvcGroupAllNoAccessImpl impl = (AggSvcGroupAllNoAccessImpl) s;
				services.put(processor.getStatementName(), impl.getMethods());
			}
		}
	}

	public static void printSerivces() {
		System.out.println("size now:" + services.size());
		for (String key : services.keySet()) {
			System.out.println("first:" + key);
			for (AggregationMethod method : services.get(key)) {
				System.out.println(method.getValue());
			}

		}
		SerializerUtil.getInstance().doserializeMap(services);
	}

	public static Map<String, AggregationMethod[]> getServices() {
		return services;
	}

}
